package com.yu.Controller;

import com.yu.POJO.Course;
import com.yu.POJO.Sc;
import com.yu.POJO.Student;
import com.yu.Service.StuService;
import com.yu.tool.MyJson;
import com.yu.tool.pageTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *  学生控制层
 * @Author: LittleFisher
 * @Date: 2021/05/30/21:24
 * @Description:
 */
@Controller
@RequestMapping("stu")
public class StuController {
    @Autowired
    StuService stuService;
    //to页面

    /**
     * 去学生基本信息页面
     * @return
     */
    @RequestMapping("stuInfo")
    public String stuInfo(){
        return "stu/stuInfo";
    }

    /**
     * 去学生选课页面
     * @return
     */
    @RequestMapping("toStuSc")
    public String toStuSc(){
        return "stu/StuSc";
    }

    /**
     * 去学生选选修页面
     * @return
     */
    @RequestMapping("toSelectCourse")
    public String toSelectCourse(){
        return "stu/SelectCourse";
    }

    /**
     * 去学生修改密码页面
     * @return
     */
    @RequestMapping("toStuUpPass")
    public String toStuUpPass(){
        return "stu/stuUpPass";
    }

    /**
     *  去查询成绩页面
     * @return
     */
    @RequestMapping("toGetGrade")
    public String toGetGrade(){
        return "stu/getGrade";
    }

    //crud Information

    @RequestMapping("getGrade")
    @ResponseBody
    public List<Sc> getGrade(int id){
        return stuService.getStuSc(id);
    }

    /**
     * 获取学生(id)的基本信息
     * @param id 学生id
     * @return Student
     */
    @RequestMapping("getStuInfo")
    @ResponseBody
    public List<Student> getStuInfo(@RequestParam int id) {
        Student student = stuService.findStuByID(id);
        List<Student> list = new ArrayList<Student>();
        list.add(student);
        return list;
    }

    /**
     * 获取学生(id)的课程记录
     * @param id 学生id
     * @return List<Course> 课程集合
     */
    @RequestMapping("getStuSc")
    @ResponseBody
    public List<Course> getStuSc(@RequestParam int id) {
        List<Course> list = stuService.getStuCourses(id);
        return list;
    }

    /**
     * 获取所有选修课信息
     * @param page 当前分页页码
     * @param rows 一页多少行
     * @return 选修课的集合(rows) 和总数(Total)
     */
    @RequestMapping("/getAllCourseBySelect")
    @ResponseBody
    public pageTool getAllCourseBySelect(int page, int rows){
        return stuService.getSelectCourse(page,rows,"选修");
    }

    /**
     * 学生(id)加入选修课程(id)
     * @param id 课程id
     * @param stu_id 学生id
     * @return MyJson msg 成功失败 error 错误信息
     */
    @RequestMapping("/joinSelectCourse")
    @ResponseBody
    public MyJson joinSelectCourse(int id,int stu_id){
        return stuService.joinSelectCourse(id,stu_id);
    }

    /**
     *
     * @param id 学生id
     * @param old 旧密码
     * @param newp 新密码
     * @return MyJson msg 成功失败 error 错误信息
     */
    @RequestMapping("stuUpPass")
    @ResponseBody
    public MyJson stuUpPass(int id,String old,String newp){
        return stuService.stuUpPass(id, old, newp);
    }
}
